/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.herenit.beijing.poc.empi.model;

import com.herenit.beijing.poc.empi.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Obama
 */
public class TableModelForList<T> extends AbstractTableModel {

    private List<List> lists = new ArrayList<List>();
    private List<T> datas = new ArrayList<T>();
    private List<ColumnName> columnNames = new ArrayList<ColumnName>();

    public TableModelForList(List<T> datas, List<ColumnName> columnNames) throws IllegalArgumentException, IllegalAccessException {
        this(datas);
        this.columnNames = columnNames;
    }

    public TableModelForList(List<T> datas) throws IllegalArgumentException, IllegalAccessException {
        this.datas = datas;
        for (T data : datas) {
            List<Object> rows = new ArrayList<Object>();
            for (Field field : data.getClass().getDeclaredFields()) {
                for(ColumnName cn:columnNames){
                    if(cn.getFiledName().equals(field.getName())){
                        field.setAccessible(true);   
                        rows.add(field.get(data));
                        break;
                    }
                }    
            }
            lists.add(rows);
        }
    }

    public TableModelForList(List<T> datas, ColumnName[] columnNames) throws IllegalArgumentException, IllegalAccessException {
        this(datas);
        this.columnNames.addAll(Arrays.asList(columnNames));
    }

    @Override
    public int getRowCount() {
        return lists.size();
    }

    @Override
    public int getColumnCount() {
        return lists.get(0).size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return lists.get(rowIndex).get(columnIndex);
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return lists.get(0).get(columnIndex).getClass();
    }

    @Override
    public String getColumnName(int column) {
        return columnNames.get(column).getDisplayName();
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        lists.get(rowIndex).set(columnIndex, aValue);
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public class ColumnName {

        private String filedName;
        private String displayName;

        public ColumnName(String filedName, String displayName) {
            this.filedName = filedName;
            this.displayName = displayName;
        }

        public ColumnName() {
        }

        public String getDisplayName() {
            return displayName;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public String getFiledName() {
            return filedName;
        }

        public void setFiledName(String filedName) {
            this.filedName = filedName;
        }
    }
}
